package com.example.mapinformationandroid.Http;

import android.util.Log;

import com.example.mapinformationandroid.ConstantData.Define;
import com.example.mapinformationandroid.Model.Information;
import com.example.mapinformationandroid.Model.User;
import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.obs.services.exception.ObsException;
import com.obs.services.model.AccessControlList;
import com.obs.services.model.HeaderResponse;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectRequest;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class OBSBASE {
    private ObsClient client;


    public OBSBASE() {
        client = new ObsClient(Define.HuaWeiObsDefine.OBS_ACCESS_KEY, Define.HuaWeiObsDefine.OBS_SECRET_KEY, OBSConfig());
    }

    //配置OBS
    private ObsConfiguration OBSConfig() {
        ObsConfiguration config = new ObsConfiguration();
        config.setEndPoint(Define.HuaWeiObsDefine.OBS_END_POINT);
        config.setSocketTimeout(30000);
        config.setMaxErrorRetry(1);

        return config;
    }

    //上传图片
    public void commitPicture(Information information) {
        try {
            // 调用接口进行操作，例如上传对象，其中localfile为待上传的本地文件路径，需要指定到具体的文件名
            PutObjectRequest request = new PutObjectRequest();
            request.setBucketName(Define.HuaWeiObsDefine.OBS_BUCKET_NAME);
            request.setObjectKey(information.getBitmap_path());
            request.setFile(new File(information.getBitmap_local_path())); // localfile为待上传的本地文件路径，需要指定到具体的文件名
            request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);       // 设置对象访问权限为公共读

            HeaderResponse response = client.putObject(request);
            Log.d("PutObject", response.toString());
        } catch (ObsException e) {
            Log.e("addInformationOBS", "Response Code: " + e.getResponseCode());
            Log.e("addInformationOBS", "Error Message: " + e.getErrorMessage());
            Log.e("addInformationOBS", "Error Code:       " + e.getErrorCode());
            Log.e("addInformationOBS", "Request ID:      " + e.getErrorRequestId());
            Log.e("addInformationOBS", "Host ID:           " + e.getErrorHostId());
        } catch (Exception e) {
            Log.e("PutObject", "Response Code: " + e.toString());
        }
    }
    public void commitPicture(User user) {
        try {
            // 调用接口进行操作，例如上传对象，其中localfile为待上传的本地文件路径，需要指定到具体的文件名
            PutObjectRequest request = new PutObjectRequest();
            request.setBucketName(Define.HuaWeiObsDefine.OBS_BUCKET_NAME);
            request.setObjectKey(user.getBitmap_path());
            request.setFile(new File(user.getBitmap_local_path())); // localfile为待上传的本地文件路径，需要指定到具体的文件名
            request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);       // 设置对象访问权限为公共读

            HeaderResponse response = client.putObject(request);
            Log.d("PutObject", response.toString());
        } catch (ObsException e) {
            Log.e("addInformationOBS", "Response Code: " + e.getResponseCode());
            Log.e("addInformationOBS", "Error Message: " + e.getErrorMessage());
            Log.e("addInformationOBS", "Error Code:       " + e.getErrorCode());
            Log.e("addInformationOBS", "Request ID:      " + e.getErrorRequestId());
            Log.e("addInformationOBS", "Host ID:           " + e.getErrorHostId());
        } catch (Exception e) {
            Log.e("PutObject", "Response Code: " + e.toString());
        }
    }


    public void OBSClose() {
        try {
            client.close();
        } catch (IOException e) {
            Log.e("addInformationOBS", "Response Code: " + e.toString());
        }

    }


    public ByteArrayOutputStream downloadPicture(Information information) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObsObject obsObject = client.getObject(Define.HuaWeiObsDefine.OBS_BUCKET_NAME, information.getPicture());

// 读取对象内容
            Log.i("GetObject", "Object content:");
            InputStream input = obsObject.getObjectContent();
            byte[] b = new byte[1024];

            int len;
            while ((len = input.read(b)) != -1) {
                bos.write(b, 0, len);
            }

            Log.i("GetObject", new String(bos.toByteArray()));
            bos.close();

        } catch (ObsException e) {
            Log.e("OBSDownload", "Response Code: " + e.getResponseCode());
            Log.e("OBSDownload", "Error Message: " + e.getErrorMessage());
            Log.e("OBSDownload", "Error Code:       " + e.getErrorCode());
            Log.e("OBSDownload", "Request ID:      " + e.getErrorRequestId());
            Log.e("OBSDownload", "Host ID:           " + e.getErrorHostId());
        } catch (IOException e) {
            Log.e("OBSDownload", "Response Code: " + e.toString());
        }
        return bos;

    }
}
